﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Reflection;

namespace HNXJ.Utils
{
    /// <summary>
    /// App.config操作类
    /// </summary>
    public static class ConfigHelper
    {
        /// <summary>
        /// 读取AppSettings下所有的值
        /// </summary>
        /// <returns>AppSettings下值数组</returns>
        public static string[] ReadAllSettings()
        {
            string[] values = {};
            try
            {
                var appSettings = ConfigurationManager.AppSettings;

                if (appSettings.Count == 0)
                {
                    Console.WriteLine("AppSettings is empty.");
                    values = null;
                }
                else
                {
                    values = new string[appSettings.Count];
                    var i = 0;
                    foreach (var key in appSettings.AllKeys)
                    {
                        Console.WriteLine("Key: {0} Value: {1}", key, appSettings[key]);
                        values[i++] = appSettings[key];
                    }
                }
            }
            catch (ConfigurationErrorsException)
            {
                Console.WriteLine("Error reading app settings");
            }
            return values;
        }
        /// <summary>
        /// 获取指定key的Setting值
        /// </summary>
        /// <param name="key"></param>
        /// <returns>指定key的Setting值</returns>
        public static string ReadSetting(string key)
        {
            string result = null;
            try
            {
                var appSettings = ConfigurationManager.AppSettings;
                result = appSettings[key] ?? string.Empty;
                Console.WriteLine(result);
            }
            catch (ConfigurationErrorsException)
            {
                Console.WriteLine("Error reading app settings");
            }
            return result;
        }
        /// <summary>
        /// 更新或添加AppSetting的Settings
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddUpdateAppSettings(string key, string value)
        {
            try
            {
                var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var settings = configFile.AppSettings.Settings;
                if (settings[key] == null)
                {
                    settings.Add(key, value);
                }
                else
                {
                    settings[key].Value = value;
                }
                configFile.Save(ConfigurationSaveMode.Modified);
               // ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
                ConfigurationManager.RefreshSection("appSettings");
            }
            catch (ConfigurationErrorsException)
            {
                Console.WriteLine("Error writing app settings");
            }
        }
        public static void ChangeConfiguration(string key, string value)
        {
            //读取程序集的配置文件
            string assemblyConfigFile = Assembly.GetEntryAssembly().Location;
            string appDomainConfigFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //获取appSettings节点
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");

            //删除name，然后添加新值
            appSettings.Settings.Remove(key);
            appSettings.Settings.Add(key, value);

            //保存配置文件
            config.Save();
            //config.Save(ConfigurationSaveMode.Modified);
            // ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
            //ConfigurationManager.RefreshSection("appSettings");
        }
    }
}
